from django.http import Http404
from django.shortcuts import render

# Create your views here.
from django.views.generic import DetailView, ListView

from article.models import Article


class ArticleDetail(DetailView):
    model = Article
    template_name = 'article/article_detail.html'

    def get(self, request, *args, **kwargs):
        response = super(ArticleDetail, self).get(request, *args, **kwargs)
        self.object.add_views()
        return response

    # def get_object(self, queryset=None):
    #     if queryset is None:
    #         queryset = self.get_queryset()
    #
    #     pk = self.kwargs.get('pk')
    #     if pk is not None:
    #         queryset = queryset.filter(pk=pk)
    #
    #     try:
    #         # Get the single item from the filtered queryset
    #         obj = queryset.get()
    #     except queryset.model.DoesNotExist:
    #         raise Http404(_("No %(verbose_name)s found matching the query") %
    #                       {'verbose_name': queryset.model._meta.verbose_name})
    #
    #     # if obj.slug != self.kwargs.get('name'):
    #     #     raise Http404()
    #     return obj


class ArticleMipDetail(DetailView):
    model = Article
    template_name = 'mip/article_detail.html'

    def get(self, request, *args, **kwargs):
        response = super(ArticleMipDetail, self).get(request, *args, **kwargs)
        self.object.add_views()
        return response


class ArticleList(ListView):
    model = Article
    paginate_by = 10
    template_name = 'article/article_list.html'
    ordering = 'updated'

    def get_queryset(self):
        self.queryset = Article.objects.filter(category__slug=self.kwargs.get('slug')).all()
        return super(ArticleList, self).get_queryset()
